package com.vf.admin.client.service.impl;


import com.vf.admin.client.dao.*;
import com.vf.admin.client.dto.AppRankingDto;
import com.vf.admin.client.service.AppFamilyService;
import com.vf.admin.client.tx.utils.TxIm;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class AppFamilyServiceImpl implements AppFamilyService {

    @Autowired
    private AppFamilyDao appFamilyDao;
    @Autowired
    private AppUserDao appUserDao;
    @Autowired
    private AppMingXiZuanShiDao appMingXiZuanShiDao;
    @Autowired
    private AppFamilyMemberDao appFamilyMemberDao;
    @Autowired
    private AppFamilySetLevelDao appFamilySetLevelDao;
    @Autowired
    private AppFamilyPrestigeDao appFamilyPrestigeDao;
    @Autowired
    private AppSetupDao appSetupDao;


    @Override
    public void save(AppFamily appFamily) {
        appFamilyDao.save(appFamily);
    }

    @Override
    public AppFamily findByLeaderId(Integer leaderId) {
        return appFamilyDao.findByLeaderId(leaderId);
    }

    @Override
    public AppFamily findByLeaderId1(Integer leaderId) {
        return appFamilyDao.findByLeaderId1(leaderId);
    }

    @Override
    public AppFamily findbyId(Integer id) {
        return appFamilyDao.findById(id);
    }

    @Override
    public AppFamily findFamilyById(Integer id){
        return appFamilyDao.findFamilyById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean createFamily(Integer userid, Integer createFamilyForDiamonds, AppFamily appFamily) {
        AppUser appUser = appUserDao.lockGet(userid);
        if (createFamilyForDiamonds != 0) {
            if (appUser.getDiamonds().compareTo(new BigDecimal(createFamilyForDiamonds)) < 0) {
                return false;
            }
            //减少钻石数量
            appUserDao.upJianZuanShi(userid, new BigDecimal(createFamilyForDiamonds));
            //增加钻石明细
            AppMingXiZuanShi appMingXiZuanShi = new AppMingXiZuanShi();
            appMingXiZuanShi.setUserid(userid);
            appMingXiZuanShi.setPic(appUser.getPic());
            appMingXiZuanShi.setNick(appUser.getNick());
            appMingXiZuanShi.setPhone(appUser.getLoginname());
            appMingXiZuanShi.setNum(new BigDecimal(createFamilyForDiamonds.toString()));
            appMingXiZuanShi.setLeixing(2);
            appMingXiZuanShi.setBeizhu("创建家族");
            appMingXiZuanShi.setCreatetime(new Date());
            appMingXiZuanShiDao.saveOrUpdate(appMingXiZuanShi);
        }

        appFamily.setLeaderid(userid);
        appFamily.setCreatetime(new Date());
        appFamily.setUpdatetime(new Date());
//        appFamily.setAddrshi(appUser.getShengid());
        appFamily.setLon(appUser.getLon());
        appFamily.setLat(appUser.getLat());
        appFamily.setStatus(3);
        appFamilyDao.save(appFamily);

        //同步将创建者加入成员表
        AppFamilyMember appFamilyMember = new AppFamilyMember();
        appFamilyMember.setFid(appFamily.getId());
        appFamilyMember.setFamilyid(appFamily.getFamilyid());
        appFamilyMember.setIdentity(1);
        appFamilyMember.setStatus(1);
        appFamilyMember.setUserid(userid);
        appFamilyMember.setCreatetime(new Date());
        appFamilyMember.setUpdatetime(new Date());
        appFamilyMemberDao.add(appFamilyMember);

        return true;
    }

    @Override
    public void familyMemberInc(Integer id) {
        appFamilyDao.familyMemberInc(id);
    }

    @Override
    public void familyMemberDec(Integer id) {
        appFamilyDao.familyMemberDec(id);
    }

    @Override
    public List<AppFamily> getFamilyList(String lon, String lat, Integer shiid, Integer type, Integer userId, SplitPageBean splitPageBean) {
        return appFamilyDao.getFamilyList(lon, lat, shiid, type, userId, splitPageBean);
    }

    @Override
    public void prestigeNumInc(Integer fid, Integer num) {
        appFamilyDao.prestigeNumInc(fid, num);
        //威望值增加同时判断是否具备升级条件
        AppFamily byId = appFamilyDao.findById(fid);
        if (byId != null) {
            List<AppFamilySetLevel> levelList = appFamilySetLevelDao.getLevelList();
            for (AppFamilySetLevel level : levelList) {
                if (byId.getPrestigenum().compareTo(level.getMinPrestige()) >= 0 && byId.getPrestigenum().compareTo(level.getMaxPrestige()) <= 0) {
                    if (byId.getFamilylevel().compareTo(level.getLevel()) != 0) {
                        appFamilyDao.updateFamilyLevel(fid, level.getLevel());
                    }
                }
            }
        }
    }

    @Override
    public Integer findRankingByPrestige(Integer fid, Integer type) {
        return appFamilyDao.findRankingByPrestige(fid, type);
    }

    /**
     * 获取排行榜
     * @param secondType
     * @return
     */
    @Override
    public List<AppRankingDto> getRanking(Integer secondType) {
        List<AppFamilyPrestige> appFamilyPrestiges = new ArrayList<>();
        ArrayList<AppRankingDto> appRankingDtos = new ArrayList<>();
        switch (secondType){
            case 1:
                appFamilyPrestiges = appFamilyPrestigeDao.getDayList();
                break;
            case 2:
                appFamilyPrestiges = appFamilyPrestigeDao.getWeekList();
                break;
            case 3:
                appFamilyPrestiges = appFamilyPrestigeDao.getMonthList();
                break;
            default:
                return null;
        }
        if(appFamilyPrestiges!=null && appFamilyPrestiges.size()>0){
            appFamilyPrestiges.forEach(e->{
                AppFamily byId = appFamilyDao.findById(e.getFid());
                if(byId != null) {
                    AppRankingDto appRankingDto = new AppRankingDto();
                    appRankingDto.setFamilyHeadPic(byId.getPic());
                    appRankingDto.setFamilyLevel(byId.getFamilylevel());
                    appRankingDto.setFamilyName(byId.getFamilyname());
                    appRankingDto.setTotalPrestige(new BigDecimal(byId.getPrestigenum()));
                    appRankingDto.setFamilyNotice(byId.getFamilynotice());
                    appRankingDto.setId(byId.getId());
                    appRankingDtos.add(appRankingDto);
                }
            });
            return appRankingDtos;
        }
        return null;
    }

    @Override
    public void saveFamilyNotice(Integer id, String familyNotice) {
        appFamilyDao.saveFamilyNotice(id,familyNotice);
    }

    @Override
    public void saveUploadPic(Integer id, String pic) {
        appFamilyDao.saveUploadPic(id,pic);
    }

    @Override
    public void saveFamilyName(Integer id, String familyName) {
        appFamilyDao.saveFamilyName(id,familyName);
    }

    @Override
    public void kickOutMember(Integer id,Integer familyMemberId)
    {
        appFamilyDao.familyMemberDec(id);
        appFamilyMemberDao.updateMemberStatus(familyMemberId,2);
    }

    @Override
    public void familyDismiss(Integer id) {
        appFamilyDao.saveFamilyStatus(id,2);
        appFamilyMemberDao.updateMemberDismiss(id,2);
    }

    @Override
    public List<AppFamily> getAfterFamilyList(SplitPageBean splitPageBean, AppFamily appFamily) {
        List<AppFamily> afterFamilyList = appFamilyDao.getAfterFamilyList(splitPageBean, appFamily);
        if(afterFamilyList!=null && afterFamilyList.size()>0){
            afterFamilyList.forEach(e->{
                AppUser appUser = appUserDao.get(e.getLeaderid());
                e.setLeaderName(appUser.getNick());
                e.setLeaderPhone(appUser.getLoginname());
            });
        }
        return afterFamilyList;
    }

    @Override
    public void saveFamilyStatus(Integer id, Integer status) {
        appFamilyDao.saveFamilyStatus(id,status);
    }

    @Override
    public AppFamily findByIdAll(Integer id)
    {
        return appFamilyDao.findByIdAll(id);
    }


    /**
     * 审核家族
     * @param appFamily
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void verifyFamily(AppFamily appFamily) {
        AppUser appUser = appUserDao.lockGet(appFamily.getLeaderid());
        AppSetup setup = appSetupDao.get();
        appFamilyDao.verifyFamily(appFamily);
        if(appFamily.getVerifyType() == 2){
            //被拒绝增加钻石数量
            appUserDao.updateZuanShi(appFamily.getLeaderid(), new BigDecimal(setup.getCreateFamilyForDiamonds()));
            //增加钻石明细
            AppMingXiZuanShi appMingXiZuanShi = new AppMingXiZuanShi();
            appMingXiZuanShi.setUserid(appFamily.getLeaderid());
            appMingXiZuanShi.setPic(appUser.getPic());
            appMingXiZuanShi.setNick(appUser.getNick());
            appMingXiZuanShi.setPhone(appUser.getLoginname());
            appMingXiZuanShi.setNum(new BigDecimal(setup.getCreateFamilyForDiamonds().toString()));
            appMingXiZuanShi.setLeixing(1);
            appMingXiZuanShi.setBeizhu("创建家族审核未通过返还");
            appMingXiZuanShi.setCreatetime(new Date());
            appMingXiZuanShiDao.saveOrUpdate(appMingXiZuanShi);
            appFamilyMemberDao.updateMemberStatus(appFamily.getId(),3,appFamily.getLeaderid());

            TxIm.destroyGroup(appFamily.getFamilyid().toString());
        }else if(appFamily.getVerifyType() ==1){
            //同步将创建者加入成员表
            /*AppFamilyMember appFamilyMember = new AppFamilyMember();
            appFamilyMember.setFid(appFamily.getId());
            appFamilyMember.setFamilyid(appFamily.getFamilyid());
            appFamilyMember.setIdentity(1);
            appFamilyMember.setStatus(1);
            appFamilyMember.setUserid(appFamily.getLeaderid());
            appFamilyMember.setCreatetime(new Date());
            appFamilyMember.setUpdatetime(new Date());
            appFamilyMemberDao.add(appFamilyMember);*/
        }

    }

}
